#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
启动 Geppetto 后端服务器
"""

import os
import sys
import logging
from geppetto_server import start_server

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)


def main():
    """主函数"""
    # 获取端口号（默认 8888）
    port = int(os.environ.get('PORT', 8888))
    
    logger.info("=" * 60)
    logger.info("Geppetto 后端服务器")
    logger.info("=" * 60)
    logger.info(f"端口: {port}")
    logger.info(f"WebSocket URL: ws://localhost:{port}/geppetto")
    logger.info(f"Health Check URL: http://localhost:{port}/health")
    logger.info("=" * 60)
    logger.info("按 Ctrl+C 停止服务器")
    logger.info("=" * 60)
    
    try:
        start_server(port=port)
    except KeyboardInterrupt:
        logger.info("\n服务器已停止")
        sys.exit(0)
    except Exception as e:
        logger.error(f"服务器错误: {e}", exc_info=True)
        sys.exit(1)


if __name__ == '__main__':
    main()

